﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Application.Seedwork;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.StockModule
{
    public class StockAllocateAppService : BaseAppService<StockAllocate, string, StockAllocateSearch, IStockAllocateRepository>, IStockAllocateAppService
    {
        private IStockInOutAppService StockInOutAppService { get; set; }
        private IStockAppService StockAppService { get; set; }
        private IStockInOutTypeAppService StockInOutTypeAppService { get; set; }
        private IWareHouseAppService WareHouseAppService { get; set; }

        public override BoolGenericResult<StockAllocate> AddOrUpdate(StockAllocate entity)
        {
            Init();
            BoolGenericResult<StockAllocate> result = new BoolGenericResult<StockAllocate>();
            this.StockAppService = this.SpringContext.GetObject(StockModuleConst.STOCKAPPSERVICE) as IStockAppService;
            this.StockInOutTypeAppService = this.SpringContext.GetObject(StockModuleConst.STOCKINOUTTYPEAPPSERVICE) as IStockInOutTypeAppService;
            this.WareHouseAppService = this.SpringContext.GetObject(StockModuleConst.WAREHOUSEAPPSERVICE) as IWareHouseAppService;

            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            StockManager stockManager = new StockManager(employee);

            StockAllocate oldStockAllocate = null;
            //加载数据
            if (entity.Id.IsNotEmpty())
            {
                oldStockAllocate = this.Repository.Get(entity.Id);
            }
            if (entity.StockIn.WareHouse.IsNotNull() && entity.StockIn.WareHouse.Id.IsNotEmpty())
            {
                entity.StockIn.WareHouse = this.WareHouseAppService.GetById(entity.StockIn.WareHouse.Id);
            }
            if (entity.StockOut.WareHouse.IsNotNull() && entity.StockOut.WareHouse.Id.IsNotEmpty())
            {
                entity.StockOut.WareHouse = this.WareHouseAppService.GetById(entity.StockOut.WareHouse.Id);
            }
            //获取所有有效库存
            foreach (StockInOutDetail siod in entity.StockOut.StockInOutDetails)
            {
                siod.Stock = this.StockAppService.GetById(siod.Stock.Id);
                siod.Stock.StockByWareHouseReports = this.StockAppService.GetStocksByWareHouseReport(entity.StockOut.WareHouse.Id, siod.Stock.Id);
            }

            StockInOutType type = this.StockInOutTypeAppService.GetById(SystemIdentifier.DefaultStockInOutType.PurchaseReturn.GetHashCode());

            if (oldStockAllocate.IsNull())
            {
                IIDGenerateAppService idGenerateAppService = this.SpringContext.GetObject(SystemModuleConst.IDGENERATEAPPSERVICE, new object[] { employee.Corporation }) as IIDGenerateAppService;
                entity.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.StockAllocate);

                //库存调拨
                result = stockManager.AllocateStock(entity.Id, entity.AllocateDate, entity.StockOut.WareHouse, entity.StockIn.WareHouse, entity.Remark,
                    string.Empty, type, type, entity.StockOut.StockInOutDetails);

                if (result.IsSuccess)
                {
                    result.Object.StockIn.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.StockIn);
                    result.Object.StockOut.Id = idGenerateAppService.GenerateID(SystemIdentifier.ReceiptCodeRuleType.StockOut);
                    base.Add(result.Object);
                }
            }
            result.Messages.Add("调拨成功");

            return result;
        }
    }
}
